package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.pojo.Vip;
import com.chixing.service.PointService;
import com.chixing.service.VipService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class VipController {
    @Autowired
    private VipService vipService;
    @Autowired
    private PointService pointService;

    @GetMapping("vip/{cusId}")
    public ModelAndView getByCusId(HttpSession httpSession){
        ModelAndView mav = new ModelAndView();
        Customer customer = (Customer) httpSession.getAttribute("loginCustomer");
        Integer cusId = customer.getCusId();
        ServerResponse vipServer = vipService.getByCusId(cusId);
        ServerResponse pointServer = pointService.getByCusId(cusId);
        if (vipServer.getCode() ==200){
            mav.addObject("pointServer",pointServer);
            mav.addObject("vipServer",vipServer);
            mav.setViewName("vip/vip");
            return mav;
        }else {
            mav.setViewName("vip/vip");
            return mav;
        }
    }
    @GetMapping("vip/update/{vipId}")
    public ModelAndView getByVipId(@PathVariable("vipId") Integer vipId){
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse1 = vipService.getByVipId(vipId);
        if (serverResponse1.getCode()==200){
            mav.addObject("serverResponse1",serverResponse1);
            mav.setViewName("vip/vipSave");
        }
        return mav;
    }
    @PutMapping("vip/update")
    @ResponseBody
    public String update(Vip vip){
        ServerResponse serverResponse1 = vipService.update(vip);
         return "redirect:/vip/vip"+vip.getCusId();
    }

    @PostMapping("vip/vip")
    @ResponseBody
    public ServerResponse save(Vip vip){
        return vipService.save(vip);
    }

    @PostMapping("svaeVip")
    public String saveVip(Integer vipId,HttpSession httpSession,String vipType,String vipStartDate,String vipEndDate,Float vipPrice){
        System.out.println("vipId : " +vipId);
        System.out.println("vipStartDate : " +vipStartDate);
        System.out.println("vipEndDate : " +vipEndDate);
        System.out.println("vipPrice : " +vipPrice);
        System.out.println("vipType : " +vipType);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = sdf.parse(vipStartDate);
            endDate = sdf.parse(vipEndDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Customer customer =(Customer) httpSession.getAttribute("loginCustomer");
        System.out.println("目前登录的用户id:" + customer.getCusId());
        Vip vip = new Vip();
        vip.setVipStartDate(startDate);
        vip.setCusId(customer.getCusId());
        vip.setVipEndDate(endDate);
        vip.setVipId(vipId);
        vip.setVipType(vipPrice.toString());
        vip.setVipType(vipType);
        Integer orderNum = UUID.randomUUID().hashCode();
        vip.setVipNumber(orderNum); // 订单编号
        vip.setVipOther1(new Date().toString());
        ServerResponse serverResponse = vipService.save(vip);
        System.out.println("返回的结果是：" + serverResponse);

        return "redirect:myNewOrder?orderNum="+orderNum;
    }


}
